package com.nit.mapper;

import com.nit.dto.StockInDTO;
import com.nit.entity.GoodsEntity;
import com.nit.entity.StockInEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

public interface StockInMapper {
    @Select("<script> " +
            "SELECT COUNT(*) " +
            "FROM tb_goods,tb_stock_in\n" +
            "WHERE tb_goods.goods_id=tb_stock_in.goods_id\n" +
            "AND tb_stock_in.user_id= #{userId}" +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "</script> ")
    Long count4Table(@Param("goodsName") Object goodsName,
                     @Param("goodsCate") Object goodsCate,
                     @Param("userId") Object userId);

    @Select("<script> " +
            "SELECT tb_stock_in.si_count,tb_stock_in.goods_id,tb_goods.goods_name,tb_stock_in.gmt_create\n" +
            "FROM tb_goods,tb_stock_in\n" +
            "WHERE tb_goods.goods_id=tb_stock_in.goods_id\n" +
            "AND tb_stock_in.user_id= #{userId}" +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "<if test='goodsCate != null'> AND goods_cate = #{goodsCate} </if>" +
            "ORDER BY tb_stock_in.si_id DESC " +
            "LIMIT #{start}, #{length} " +
            "</script> ")
    List<StockInDTO> list4Table(@Param("start")Integer start,
                                 @Param("length") Integer length,
                                 @Param("goodsName") Object goodsName,
                                 @Param("goodsCate") Object goodsCate,
                                 @Param("userId") Object userId);
    /**
    * @Description: 添加记录
    * @Author: Lc
    * @Date: 2019/9/19
    */
        @Select(
            "SELECT shop_id " +
            "FROM tb_shop " +
            "WHERE user_id= #{userId}")
    Integer getShop(@Param("userId")Object userId )throws Exception;

    @Insert("INSERT INTO " +
            "tb_stock_in (" +
            "shop_id, goods_id, " +
            "si_count, user_id, " +
            "gmt_create) " +
            "VALUES ("  +
            "#{stockInEntity.shopId},"+
            "#{stockInEntity.goodsId}, " +
            "#{stockInEntity.siCount}, #{stockInEntity.userId}, " +
            "#{stockInEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "siId", keyColumn = "si_id")
    Long add(@Param("stockInEntity") StockInEntity stockInEntity);

}
